# -*- coding:utf-8 -*-

from fake_useragent import UserAgent
from lxml import etree
import time
import base64
import requests

from requests.auth import HTTPProxyAuth


requests.packages.urllib3.disable_warnings()  # 忽略HTTPS安全警告

"""
使用代理
阿布代理
https://www.abuyun.com/
"""



# 代理服务器
proxyHost = "http-dyn.abuyun.com"
proxyPort = "9020"
# 代理隧道验证信息
proxyUser = "H01I644F9J45ODID"
proxyPass = "C61D2EF7EE18A03A"


def get_request_proxy(method, url, headers_s, data, retry=1):
    ua = UserAgent(use_cache_server=False)  # 禁用服务器缓存
    headers_p = {
        "User-Agent": ua.random,
        # 'Proxy-Authorization': "Basic " + base64.urlsafe_b64encode(
        #     bytes((proxyUser + ":" + proxyPass), "ascii")).decode("utf8"),
        "Proxy-Authorization": "Basic " + base64.b64encode((proxyUser + ":" + proxyPass).encode('utf-8'))
            .decode()
    }
    # 合并头
    headers = dict(headers_s, **headers_p)

    proxyMeta = "http://%(user)s:%(pass)s@%(host)s:%(port)s" % {
        "host": proxyHost,
        "port": proxyPort,
        "user": proxyUser,
        "pass": proxyPass,
    }
    proxies = {
        "http": proxyMeta,
        "https": proxyMeta,
    }
    res = None
    try:
        if method == "POST":
            if url.find("https") == 0:
                res = requests.post(url, headers=headers, data=data, proxies=proxies, verify=False)
            else:
                res = requests.post(url, headers=headers, data=data, proxies=proxies)
        elif method == "GET":
            if url.find("https") == 0:
                res = requests.get(url, headers=headers, params=data, proxies=proxies, verify=False)
            else:
                res = requests.get(url, headers=headers, params=data, proxies=proxies)
        else:
            res = None
    except Exception as ex:
        print(f"-------------【错误】,重试第【{retry}】次-------------")
        print(ex)
        retry += 1
        return get_request_proxy(method, url, headers, data, retry)
    else:
        if res.status_code != 200:
            max_retry = 3  # 重试次数
            print(f"-------------返回状态码:{res.status_code},重试第【{retry}】次-------------")
            retry += 1
            return get_request_proxy(method, url, headers, data, retry)
    return res

if __name__ == '__main__':
    url = "https://ip.cn/"
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36',

    }
    # html = get_request_proxy("GET", url, headers, "")
    html = requests.get(url, headers=headers, verify=False)
    root = etree.HTML(html.text)
    print("".join(root.xpath('//div[@id="result"]//text()')))